﻿
/*
 * Gravity Gun Script
 * Equip Pistol/Desert Eagle, hold RMB to aim, then press & hold MMB to pickup object (whilst still holding RMB)
 * Pistol will pickup only one object at a time.
 * Desert Eagle will pickup everything near it!
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GTA;

namespace GravityGunScript
{
    class GGScript : GTA.ScriptBase
    {
        GravityGun GG;

        public GGScript()
        {
            new Log("GravityGun", true);
            Log.Debug("GGScript Constructor");
            GTA.Events.OnStart.Handler +=new EventHandler<EventArgs>(OnStart);
            GTA.Events.RunTick.Handler +=new EventHandler<EventArgs>(RunTick);
            GTA.Events.KeyDown.Handler +=new EventHandler<GTA.Events.KeyEventArgs>(OnKeyDown);
            GTA.Events.KeyUp.Handler +=new EventHandler<GTA.Events.KeyEventArgs>(OnKeyUp);
        }

        public void OnStart(Object sender, EventArgs args)
        {
            Log.Debug("GGScript OnStart");
            GG = new GravityGun();
        }

        public void RunTick(Object sender, EventArgs args)
        {
            if (GG.isActive) GG.Tick();
            //Log.Debug("GGScript RunTick");
        }

        unsafe public void OnKeyDown(Object sender, GTA.Events.KeyEventArgs args)
        { 
            //Log.Debug("GGScript onKeyDown: " + KeyCode.ToString());
            if (args.KeyCode == GTA.Keys.RButton) 
            {
                Log.Debug("GGScript RightMB: " + args.KeyCode.ToString());
                Player pl = new Player();
                Ped plped = pl.GetPlayerPed();
                eWeapon wep = new eWeapon();
                ScriptApi.GetCurrentCharWeapon(plped, &wep);
                Log.Debug("GGScript RightMB: wep: " + wep.ToString());
                if (wep == eWeapon.WEAPON_PISTOL)
                {
                    Log.Debug("GGScript IsAiming");
                    GG.Mode = GravityGun.GGMode.Single;
                    GG.isAiming = true;
                }
                if (wep == eWeapon.WEAPON_DEAGLE)
                {
                    Log.Debug("GGScript IsAiming");
                    GG.Mode = GravityGun.GGMode.Multiple;
                    GG.isAiming = true;
                }
            }
            if (args.KeyCode == GTA.Keys.MButton) { Log.Debug("GGScript MiddleMB"); if (GG.isAiming) GG.Start(); };
        }

        public void OnKeyUp(Object sender, GTA.Events.KeyEventArgs args)
        {
            //Log.Debug("GGScript onKeyUp: "+KeyCode.ToString());
            if (args.KeyCode == GTA.Keys.RButton) { Log.Debug("GGScript NOT IsAiming"); GG.isAiming = false; }
            if (args.KeyCode == GTA.Keys.MButton) { if (GG.isActive) GG.Stop(); };
        }

    }
}
